﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SCI_actuator
{
    public partial class Mutliservo2 : Form
    {
        private menu_control frmain;
        private xmlrw scixml;
        public Mutliservo2(menu_control fr)
        {
            InitializeComponent();
            this.frmain = fr;
            scixml = new xmlrw();
        }

        private void single_send1_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(servo_num1.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH1.Text
            + constant.REG_T_POSITION_LO + servo_posL1.Text + constant.REG_T_VELOCITY_HI + servo_timeH1.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL1.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + servo_num1.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH1.Text
            + constant.REG_T_POSITION_LO + servo_posL1.Text + constant.REG_T_VELOCITY_HI + servo_timeH1.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL1.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void single_send2_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(servo_num2.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH2.Text
            + constant.REG_T_POSITION_LO + servo_posL2.Text + constant.REG_T_VELOCITY_HI + servo_timeH2.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL2.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + servo_num2.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH2.Text
            + constant.REG_T_POSITION_LO + servo_posL2.Text + constant.REG_T_VELOCITY_HI + servo_timeH2.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL2.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void single_send3_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(servo_num3.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH3.Text
            + constant.REG_T_POSITION_LO + servo_posL3.Text + constant.REG_T_VELOCITY_HI + servo_timeH3.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL3.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + servo_num3.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH3.Text
            + constant.REG_T_POSITION_LO + servo_posL3.Text + constant.REG_T_VELOCITY_HI + servo_timeH3.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL3.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void single_send4_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(servo_num4.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH4.Text
            + constant.REG_T_POSITION_LO + servo_posL4.Text + constant.REG_T_VELOCITY_HI + servo_timeH4.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL4.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + servo_num4.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH4.Text
            + constant.REG_T_POSITION_LO + servo_posL4.Text + constant.REG_T_VELOCITY_HI + servo_timeH4.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL4.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void single_send5_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(servo_num5.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH5.Text
            + constant.REG_T_POSITION_LO + servo_posL5.Text + constant.REG_T_VELOCITY_HI + servo_timeH5.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL5.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + servo_num5.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH5.Text
            + constant.REG_T_POSITION_LO + servo_posL5.Text + constant.REG_T_VELOCITY_HI + servo_timeH5.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL5.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void single_send6_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(servo_num6.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH6.Text
            + constant.REG_T_POSITION_LO + servo_posL6.Text + constant.REG_T_VELOCITY_HI + servo_timeH6.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL6.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + servo_num6.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH6.Text
            + constant.REG_T_POSITION_LO + servo_posL6.Text + constant.REG_T_VELOCITY_HI + servo_timeH6.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL6.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void single_send7_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(servo_num7.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH7.Text
            + constant.REG_T_POSITION_LO + servo_posL7.Text + constant.REG_T_VELOCITY_HI + servo_timeH7.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL7.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + servo_num7.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH7.Text
            + constant.REG_T_POSITION_LO + servo_posL7.Text + constant.REG_T_VELOCITY_HI + servo_timeH7.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL7.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void single_send8_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray(servo_num8.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH8.Text
            + constant.REG_T_POSITION_LO + servo_posL8.Text + constant.REG_T_VELOCITY_HI + servo_timeH8.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL8.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + servo_num8.Text + "0C" + "02" + constant.REG_T_POSITION_HI + servo_posH8.Text
            + constant.REG_T_POSITION_LO + servo_posL8.Text + constant.REG_T_VELOCITY_HI + servo_timeH8.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL8.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void multi_send_Click(object sender, EventArgs e)
        {
            string sum_str = null;
            byte[] bRx = this.frmain.HexStringToByteArray("FE" + "61" + servo_num1.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH1.Text
            + constant.REG_T_POSITION_LO + servo_posL1.Text + constant.REG_T_VELOCITY_HI + servo_timeH1.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL1.Text + constant.REG_CURVE_APPEND + "02" + servo_num2.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH2.Text
            + constant.REG_T_POSITION_LO + servo_posL2.Text + constant.REG_T_VELOCITY_HI + servo_timeH2.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL2.Text + constant.REG_CURVE_APPEND + "02" + servo_num3.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH3.Text
            + constant.REG_T_POSITION_LO + servo_posL3.Text + constant.REG_T_VELOCITY_HI + servo_timeH3.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL3.Text + constant.REG_CURVE_APPEND + "02" + servo_num4.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH4.Text
            + constant.REG_T_POSITION_LO + servo_posL4.Text + constant.REG_T_VELOCITY_HI + servo_timeH4.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL4.Text + constant.REG_CURVE_APPEND + "02" + servo_num5.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH5.Text
            + constant.REG_T_POSITION_LO + servo_posL5.Text + constant.REG_T_VELOCITY_HI + servo_timeH5.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL5.Text + constant.REG_CURVE_APPEND + "02" + servo_num6.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH6.Text
            + constant.REG_T_POSITION_LO + servo_posL6.Text + constant.REG_T_VELOCITY_HI + servo_timeH6.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL6.Text + constant.REG_CURVE_APPEND + "02" + servo_num7.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH7.Text
            + constant.REG_T_POSITION_LO + servo_posL7.Text + constant.REG_T_VELOCITY_HI + servo_timeH7.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL7.Text + constant.REG_CURVE_APPEND + "02" + servo_num8.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH8.Text
            + constant.REG_T_POSITION_LO + servo_posL8.Text + constant.REG_T_VELOCITY_HI + servo_timeH8.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL8.Text + constant.REG_CURVE_APPEND + "02");
            byte sum = this.frmain.checksum(bRx);

            if (sum >= 16)
                sum_str = Convert.ToString(sum, 16);
            else
                sum_str = "0" + Convert.ToString(sum, 16);
            string senddata = "FF" + "FF" + "FE" + "61" + servo_num1.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH1.Text
            + constant.REG_T_POSITION_LO + servo_posL1.Text + constant.REG_T_VELOCITY_HI + servo_timeH1.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL1.Text + constant.REG_CURVE_APPEND + "02" + servo_num2.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH2.Text
            + constant.REG_T_POSITION_LO + servo_posL2.Text + constant.REG_T_VELOCITY_HI + servo_timeH2.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL2.Text + constant.REG_CURVE_APPEND + "02" + servo_num3.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH3.Text
            + constant.REG_T_POSITION_LO + servo_posL3.Text + constant.REG_T_VELOCITY_HI + servo_timeH3.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL3.Text + constant.REG_CURVE_APPEND + "02" + servo_num4.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH4.Text
            + constant.REG_T_POSITION_LO + servo_posL4.Text + constant.REG_T_VELOCITY_HI + servo_timeH4.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL4.Text + constant.REG_CURVE_APPEND + "02" + servo_num5.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH5.Text
            + constant.REG_T_POSITION_LO + servo_posL5.Text + constant.REG_T_VELOCITY_HI + servo_timeH5.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL5.Text + constant.REG_CURVE_APPEND + "02" + servo_num6.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH6.Text
            + constant.REG_T_POSITION_LO + servo_posL6.Text + constant.REG_T_VELOCITY_HI + servo_timeH6.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL6.Text + constant.REG_CURVE_APPEND + "02" + servo_num7.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH7.Text
            + constant.REG_T_POSITION_LO + servo_posL7.Text + constant.REG_T_VELOCITY_HI + servo_timeH7.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL7.Text + constant.REG_CURVE_APPEND + "02" + servo_num8.Text + "0A" + constant.REG_T_POSITION_HI + servo_posH8.Text
            + constant.REG_T_POSITION_LO + servo_posL8.Text + constant.REG_T_VELOCITY_HI + servo_timeH8.Text + constant.REG_T_VELOCITY_LO
            + servo_timeL8.Text + constant.REG_CURVE_APPEND + "02" + sum_str;

            if (this.frmain.send_timer.Enabled == true)
            {
                this.frmain.send_timer.Stop();
            }
            this.frmain.servosend(senddata);
            if (this.frmain.realtime_judge == false)
            {
                this.frmain.send_timer.Stop();
            }
            else if (this.frmain.realtime_judge == true)
            {
                this.frmain.send_timer.Start();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Dispose();
        }
    }
}
